package com.cater.utils;

import java.util.List;

import com.cater.sys.entity.Area;
import com.cater.sys.entity.Dep;
import com.cater.sys.entity.Functional;
import com.cater.sys.entity.Industry;
import com.cater.sys.entity.Language;

public class TreeUtil {
	/**
	 * 封装treeObject
	 * 
	 * @param list
	 *            分类表
	 * @param typeId
	 *            传入的父节点ID
	 * @return String
	 */
	public TreeObject[] getTreeObjects(List<Dep> list) {
		TreeObject[] trees = new TreeObject[list.size()];
		for (int i = 0; i < trees.length; i++) {
			TreeObject object = new TreeObject();
			Dep dep = list.get(i);
			Integer depId = dep.getDepId();
			object.setId(dep.getDepId().toString());
			object.setDepId(depId);
			object.setDepName(dep.getDepName());
			object.setAddress(dep.getAddress());
			object.setCreatetime(dep.getCreateDate());
			object.setAreaId(dep.getAreaId());
			object.setTitle(dep.getTitle());
			object.setParent(dep.getPid().toString());
			object.setLevel(dep.getLevel());
			object.setExpanded(true);
			object.setLoaded(false);
			object.setLeaf(false);
			trees[i] = object;
		}
		return trees;
	}

	/**
	 * 通用类别树形
	 * 
	 * @param list
	 * @return
	 *//*
		 * public TypeTreeObject[] getTypeTreeObjects(List<Type> list) {
		 * TypeTreeObject[] trees = new TypeTreeObject[list.size()]; for (int i = 0; i <
		 * trees.length; i++) { TypeTreeObject object = new TypeTreeObject(); Type type
		 * = list.get(i); object.setId(type.getId());
		 * object.setTypeName(type.getTypeName()); object.setSeq(type.getSeq());
		 * object.setPid(type.getPid()); if(type.getPid()==0){ object.setParent("0");
		 * }else{ object.setParent(type.getPid()+""); } if(type.getPid()==0){
		 * object.setLevel("0"); object.setLeaf(false); object.setExpanded(true);
		 * object.setLoaded(false); } else { object.setLevel("1"); object.setLeaf(true);
		 * object.setExpanded(true); object.setLoaded(true); }
		 * 
		 * trees[i] = object; } return trees; }
		 */

	public static TreeObject[] getAreaObjects(List<Area> list) {
		TreeObject[] trees = new TreeObject[list.size()];
		for (int i = 0; i < trees.length; i++) {
			TreeObject object = new TreeObject();
			Area area = list.get(i);
			object.setId(area.getId() + "");
			if (area.getFatherId() == 0) {
				object.setParent("0");
			} else {
				object.setParent(Integer.toString(area.getFatherId()));
			}
			if (area.getLevel() == 3) {
				object.setLevel(area.getLevel().toString());
				object.setLeaf(true);
				object.setExpanded(false);
				object.setLoaded(true);
			} else {
				object.setLevel(area.getLevel().toString());
				object.setLeaf(false);
				object.setExpanded(false);
				object.setLoaded(true);
			}
			object.setArea(area);
			trees[i] = object;
		}
		return trees;
	}

	/**
	 * @行业树形结构
	 * @param list
	 * @return
	 */
	public static TreeObject[] getIndustryObjects(List<Industry> list) {
		TreeObject[] trees = new TreeObject[list.size()];
		for (int i = 0; i < trees.length; i++) {
			TreeObject object = new TreeObject();
			Industry t = list.get(i);
			object.setId(t.getId() + "");
			if (t.getParentId() == "0") {
				object.setParent("0");
			} else {
				object.setParent(t.getParentId());
			}
			if (t.getLevel() == "3") {
				object.setLevel(t.getLevel());
				object.setLeaf(true);
				object.setExpanded(true);
				object.setLoaded(true);
			} else {
				object.setLevel(t.getLevel().toString());
				object.setLeaf(false);
				object.setExpanded(true);
				object.setLoaded(false);
			}
			object.setIndustry(t);
			trees[i] = object;
		}
		return trees;
	}

	/**
	 * @职能树形结构
	 * @param list
	 * @return
	 */
	public static TreeObject[] getFunctionalObjects(List<Functional> list) {
		TreeObject[] trees = new TreeObject[list.size()];
		for (int i = 0; i < trees.length; i++) {
			TreeObject object = new TreeObject();
			Functional t = list.get(i);
			object.setId(t.getId() + "");
			if (t.getParentId() == "0") {
				object.setParent("0");
			} else {
				object.setParent(t.getParentId());
			}
			if (t.getLevel().equals("3")) {
				object.setLevel(t.getLevel());
				object.setLeaf(true);
				object.setExpanded(true);
				object.setLoaded(true);
			} else {
				object.setLevel(t.getLevel().toString());
				object.setLeaf(false);
				object.setExpanded(true);
				object.setLoaded(false);
			}
			object.setFunctional(t);
			trees[i] = object;
		}
		return trees;
	}

	/**
	 * @职能树形结构
	 * @param list
	 * @return
	 */
	public static TreeObject[] getLanguageObjects(List<Language> list) {
		TreeObject[] trees = new TreeObject[list.size()];
		for (int i = 0; i < trees.length; i++) {
			TreeObject object = new TreeObject();
			Language t = list.get(i);
			object.setId(t.getId() + "");
			if (t.getParentId() == "0") {
				object.setParent("0");
			} else {
				object.setParent(t.getParentId());
			}
			if (t.getLevel().equals("2")) {
				object.setLevel(t.getLevel());
				object.setLeaf(true);
				object.setExpanded(true);
				object.setLoaded(true);
			} else {
				object.setLevel(t.getLevel().toString());
				object.setLeaf(false);
				object.setExpanded(true);
				object.setLoaded(false);
			}
			object.setLanguage(t);
			trees[i] = object;
		}
		return trees;
	}
}
